//= require "datastructure"
//= require "splitpane"
//= require "groupedlist"
//= require "ungroupedlist"
//= require "information"

/* MusicNet Alignment Tool
 *
 * Copyright (c) 2010 Joe Lambert
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * == alignment ==
 *  
 * The Main Section 
 **/

/** section: alignment
 * class AlignmentTool
 *  
 * The main AlignmentTool Controller Object
 *  
 * Copyright (c) 2010 Joe Lambert
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 **/

/**
 * new AlignmentTool()
 *  
 **/

var AlignmentTool = Class.create({
	initialize: function()
	{
		window.tool = this;
		
		this.lh_surface = $('lhscontainer');
		this.rh_surface = $('rhs');
		
		this.info_surface = $('rhs');
		
		this.ungrouped_list = new UngroupedList({
			listViewClass: 'ListViewUnGrouped'
		});
		this.ungrouped_list.focus();
		this.lh_surface.insert(this.ungrouped_list.getSurface());

		this.grouped_list = new GroupedList({
			listViewClass: 'ListViewGrouped',
			dataURL: 'ajax/grouped'
		});		
		this.lh_surface.insert(this.grouped_list.getSurface());
		
		this.infoview = new InfoView();
		this.info_surface.insert(this.infoview.getSurface());
		
		// Create keyboard shortcuts for functions
		
		Event.observe(window, 'keydown', function(event){
			
			// else if(event.keyCode == 83) // s
			// 	document.fire('data:save');
			if(event.keyCode == 9) // tab
			{
				event.stop();
				// toggle the focus
				if(this.ungrouped_list.getSurface().hasClassName('focus'))
				{
					this.ungrouped_list.getSurface().removeClassName('focus');
					this.grouped_list.getSurface().addClassName('focus');
				}
				else
				{
					this.ungrouped_list.getSurface().addClassName('focus');
					this.grouped_list.getSurface().removeClassName('focus');
				}
			}
			
			//if(event.keyCode == 38 /* UP */ || event.keyCode == 40 /* DOWN */ || event.keyCode == 39 /* RIGHT */)
			//{
				
				// Pass on the keypress to the infocus list
				if(this.ungrouped_list.getSurface().hasClassName('focus'))
					this.ungrouped_list.keyPress(event);
				else
					this.grouped_list.keyPress(event);
					
			//}
				
			//console.debug(event);
		}.bind(this));
		
		$('loading').hide();
		
		document.observe('loading:show', function(event) { this.showLoading(event.memo) }.bindAsEventListener(this));
		document.observe('loading:hide', this.hideLoading.bindAsEventListener(this));
		
		if(window.location.hash)
		{
			var h = window.location.hash.substr(1, window.location.hash.length-1);
			this.hash = h.toQueryParams();
		}
		else
			this.hash = {};
		
		if(this.hash.skin)
		{
			var fileref=document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", "css/"+this.hash.skin+".css");
			document.getElementsByTagName("head")[0].appendChild(fileref);
		}
	},
	
/**
 * AlignmentTool#showLoading(string) -> null
 * - string (String): A message to display whilst loading
 * 
 **/
	
	showLoading: function(string)
	{
		if(string && Object.isString(string))
			$('loading').update(string);
			
		$('loading').show();
	},
	
/**
 * AlignmentTool#hideLoading() -> null
 * 
 **/
	
	hideLoading: function()
	{
		$('loading').hide();
	}
});

document.observe('dom:loaded', function(){
	new AlignmentTool();
})